package com.lineage.data.cmd;

import com.lineage.config.ConfigRecord;
import com.lineage.config.Configtype;
import com.lineage.server.datatables.lock.LogEnchantReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1ItemPower;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_OwnCharStatus;
import com.lineage.server.serverpackets.S_SPMR;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.BroadcastUtil;
import com.lineage.william.GiveBack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EnchantArmor extends EnchantExecutor {
  private static final Log _log = LogFactory.getLog(EnchantArmor.class);
  
  public void failureEnchant(L1PcInstance pc, L1ItemInstance item) {
    if (pc.get_other().get_item_objid() != 0) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT物品正在进行托售中.请在重新操作一次"));
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
      pc.get_other().set_item_objid(0);
      return;
    } 
    StringBuilder s = new StringBuilder();
    if (ConfigRecord.LOGGING_BAN_ENCHANT)
      LogEnchantReading.get().failureEnchant(pc, item); 
    if (!item.isIdentified()) {
      s.append(item.getName());
    } else {
      s.append(item.getLogName());
    } 
    pc.sendPackets((ServerBasePacket)new S_ServerMessage(164, s.toString(), "$252"));
    GiveBack.savepcid.add(Integer.valueOf(pc.getId()));
    GiveBack.saveweapon.add(item);
    GiveBack.savename.add(item.getNumberedViewName(1L));
    if (!pc.isGm() && item.getEnchantLevel() - item.getItem().get_safeenchant() >= Configtype.armor_savebroad - 1)
      BroadcastUtil.broadcast(Integer.valueOf(Configtype.armorbroadfail), Configtype.msg1_fail, new Object[] { pc.getName(), s.toString() }); 
    pc.getInventory().removeItem(item, item.getCount());
    _log.info("人物:" + pc.getName() + "点爆物品" + item.getItem().getName() + " 物品OBJID:" + item.getId());
  }
  
  public void successEnchant(L1PcInstance pc, L1ItemInstance item, int randomELevel) {
    if (pc.get_other().get_item_objid() != 0) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT物品正在进行托售中.请在重新操作一次"));
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
      pc.get_other().set_item_objid(0);
      return;
    } 
    StringBuilder s = new StringBuilder();
    StringBuilder sa = new StringBuilder();
    StringBuilder sb = new StringBuilder();
    if (!item.isIdentified()) {
      s.append(item.getName());
    } else if (item.getEnchantLevel() > 0) {
      s.append("+" + item.getEnchantLevel() + " " + item.getName());
    } else if (item.getEnchantLevel() < 0) {
      s.append(String.valueOf(String.valueOf(item.getEnchantLevel())) + " " + item.getName());
    } else {
      s.append(item.getName());
    } 
    switch (randomELevel) {
      case 0:
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(160, s.toString(), "$252", "$248"));
        pc.setcheck_lv(true);
        return;
      case -1:
        sa.append("$246");
        sb.append("$247");
        break;
      case 1:
        sa.append("$252");
        sb.append("$247");
        break;
      case 2:
      case 3:
        sa.append("$252");
        sb.append("$248");
        break;
    } 
    pc.sendPackets((ServerBasePacket)new S_ServerMessage(161, s.toString(), sa.toString(), sb.toString()));
    int oldEnchantLvl = item.getEnchantLevel();
    int newEnchantLvl = oldEnchantLvl + randomELevel;
    if (oldEnchantLvl != newEnchantLvl) {
      item.setEnchantLevel(newEnchantLvl);
      pc.getInventory().updateItem(item, 4);
      pc.getInventory().saveItem(item, 4);
      if (!pc.isGm() && item.getEnchantLevel() - item.getItem().get_safeenchant() >= Configtype.armor_savebroad)
        BroadcastUtil.broadcast(Integer.valueOf(Configtype.armorbroadtrue), Configtype.msg1_true, new Object[] { pc.getName(), s.toString() }); 
      if (item.isEquipped()) {
        Integer attack, bowattack, mr, sp, weight, hp;
        int use_type = item.getItem().getUseType();
        switch (use_type) {
          case 2:
          case 18:
          case 19:
          case 20:
          case 21:
          case 22:
          case 25:
          case 47:
            pc.addAc(-randomELevel);
            attack = (Integer)L1ItemPower.Attack.get(Integer.valueOf(item.getItemId()));
            if (attack != null)
              pc.addMr(randomELevel * attack.intValue()); 
            bowattack = (Integer)L1ItemPower.BowAttack.get(Integer.valueOf(item.getItemId()));
            if (bowattack != null)
              pc.addMr(randomELevel * bowattack.intValue()); 
            mr = (Integer)L1ItemPower.MR.get(Integer.valueOf(item.getItemId()));
            if (mr != null)
              pc.addMr(randomELevel * mr.intValue()); 
            sp = (Integer)L1ItemPower.SP.get(Integer.valueOf(item.getItemId()));
            if (sp != null)
              pc.addSp(randomELevel * sp.intValue()); 
            weight = (Integer)L1ItemPower.weightReductionByEnchant.get(Integer.valueOf(item.getItemId()));
            if (weight != null)
              pc.addWeightReduction(randomELevel * weight.intValue()); 
            hp = (Integer)L1ItemPower.HP.get(Integer.valueOf(item.getItemId()));
            if (hp == null)
              break; 
            if (item.getItemId() == 300429) {
              int enchantlvl = item.getEnchantLevel();
              if (randomELevel == -1)
                enchantlvl++; 
              switch (enchantlvl) {
                case 5:
                case 7:
                case 9:
                  pc.addMaxHp(randomELevel * hp.intValue());
                  break;
              } 
              break;
            } 
            pc.addMaxHp(randomELevel * hp.intValue());
            break;
          case 23:
          case 24:
          case 37:
          case 40:
            if (item.getItem().get_greater() != 3)
              item.GreaterAtEnchant(pc, randomELevel); 
            break;
        } 
        pc.sendPackets((ServerBasePacket)new S_SPMR(pc));
        pc.sendPackets((ServerBasePacket)new S_OwnCharStatus(pc));
      } 
    } 
  }
}
